<?php

namespace app\api\controller\pifa;

use app\common\controller\Api;
use think\Config;
use think\Db;
use think\Validate;
use think\Session;
use fast\WechatMini;
use addons\third\model\Third;
use think\Log;
use think\Request;
use fast\Http;
use think\Exception;
use addons\third\library\Service;

/**
 * 首页接口
 */
class Goods extends Api
{
    protected $noNeedLogin = ['login','getconfig','getMendian1','token','getfenlei','getgoods'];
    protected $noNeedRight = ['*'];

     public function getfenlei()
    {
        
        $map['status'] = 1;
        $map['pid'] = 0;
        $list = Db::table('fa_litestore_category')->where($map)->order('weigh desc')->select();
        $this->success("成功",$list);
    }
    

    public function getgoods()
    {
        $post = $this->request->request();



        if (isset($post['type'])) {
           $map['fenlei_id'] = $post['type'];
        }

        $map['open_switch'] = '1';

        $list = Db::table('fa_pifa_goods')->where($map)->order('weigh desc,createtime desc')->paginate(12);

        $list = $list->toArray();

        foreach ($list['data'] as $key => $value) {
            $list['data'][$key]['goodsitem'] = json_decode($value['goodsitem'],true);
        }


        $this->success("成功",$list);
    }


    public function addcar()
    {
        $post = file_get_contents( "php://input");
        $post = json_decode($post,true);
        
        // $map2['status'] = '1';
        // $map2['user_id'] = $this->auth->id;
        // $mendian = Db::table('fa_pifa_jxs')->where($map2)->find();

        // if (empty($mendian)) {
        //    $this->success("请先认证");
        // }

        foreach ($post['goods_list'] as $key => $value) {


            $goods = Db::table('fa_litestore_goods')->find($value['goods_id']);

            $map['shangjia_id'] = $post['mendian_id'];
            $map['goods_id'] = $value['goods_id'];
            $map['goods_item_id'] = $value['sku_price_id'];
            $map['user_id'] = $this->auth->id;
            $res = Db::table('fa_pifa_cart')->where($map)->find();
            if (empty($res)) {
                $map['goods_num'] = $value['goods_num'];
                $map['createtime'] = time();
                $map['updatetime'] = time();
                Db::table('fa_pifa_cart')->insert($map);
            }else{
                Db::table('fa_pifa_cart')->where($map)->setInc('goods_num',$value['goods_num']);
            }
        }
        $this->success("添加成功");
    }

    public function cartlist()
    {

    }
    


    public function Detail()
    {
        $post = $this->request->request();
        $info = Db::table('fa_pifa_goods')->find($post['id']);
        $info['canshu'] = json_decode($info['canshu'],true);
        $info['images'] = explode(',',$info['images']);

        foreach ($info['images'] as $key => $value) {
            $info['images'][$key] =  $this->request->root(true) .$value;
        }
        $map['goods_id'] = $post['id'];
        $map['goods_id'] = $post['id'];
        $info['goods_item'] = Db::table('fa_pifa_goodsitem')->where($map)->select();

        $this->success("成功",$info);

    }


    
}
